package org.classg.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.cms.entity.Banner;
import org.classg.cms.service.BannerService;
import org.classg.servicebase.exception.MyException;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-29
 */
@RestController
@Api(description = "banner轮播图管理")
@RequestMapping("/bannerAdmin/banner")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private BannerService bannerService;

    @ApiOperation("获取Banner分页列表")
    @GetMapping("/queryBannerPage/{page}/{limit}")
    public R queryBannerPage(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit
    ) {
        Page<Banner> bannerPage = new Page<>(page, limit);
        bannerService.page(bannerPage, null);
        return R.ok().data("items", bannerPage.getRecords()).data("total", bannerPage.getTotal());
    }

    @ApiOperation("根据id查询Banner")
    @GetMapping("/queryBannerById/{id}")
    public R queryBannerById(@ApiParam(name = "id", value = "轮播图id", required = true) @PathVariable String id) {
        Banner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation("新增Banner")
    @PostMapping("/insertBanner")
    public R insertBanner(@ApiParam(name = "banner", value = "轮播图对象", required = true) @RequestBody Banner banner) {
        boolean temp = bannerService.save(banner);
        if (!temp) {
            throw new MyException(20001, "新增Banner失败！！！");
        } else {
            return R.ok();
        }
    }

    @ApiOperation("根据id更新Banner")
    @PutMapping("/updateBannerById")
    public R updateBannerById(@ApiParam(name = "banner", value = "轮播图对象", required = true) @RequestBody Banner banner) {
        boolean temp = bannerService.updateById(banner);
        if (!temp) {
            throw new MyException(20001, "根据id更新Banner失败了！！！");
        } else {
            return R.ok();
        }
    }

    @ApiOperation("根据id删除Banner")
    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBannerById(@ApiParam(name = "id", value = "轮播图id", required = true) @PathVariable String id) {
        boolean temp = bannerService.removeById(id);
        if (!temp) {
            throw new MyException(20001, "根据id删除Banner失败了！！！");
        } else {
            return R.ok();
        }
    }
}

